<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Delete All : DataMapper ORM - User Guide</title>

<link rel="shortcut icon" type="image/png" href="../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Delete All
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Delete All</h1>

<p>Delete All is used to delete all objects in an objects all list. It is basically quicker than looping through the all list yourself to delete each one. For example:</p>

<pre>
<samp>// Get a number of books from the year 2000
</samp><var>$b </var><kbd>= new </kbd><var>Book</var><kbd>();
</kbd><var>$b</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'year'</dfn><kbd>, </kbd><var>2000</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Loop through the all list and delete them one by one
</samp><kbd>foreach (</kbd><var>$b</var><kbd>-&gt;</kbd><var><i>all</i> </var><kbd>as </kbd><var>$book</var><kbd>)
{
    </kbd><var>$book</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>();
}</kbd>
</pre>

<p>Instead just do this:</p>
<pre>
<var>$b </var><kbd>= new </kbd><var>Book</var><kbd>();
</kbd><var>$b</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'year'</dfn><kbd>, </kbd><var>2000</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();
</kbd><var>$b</var><kbd>-&gt;</kbd><var><u>delete_all</u></var><kbd>();</kbd>
</pre>

<p>This is especially useful for deleting related items.</p>

<p>&nbsp;</p>

<h1>Truncate</h1>

<p>Since Delete All will iterate over all objects in an objects all list, it will run a delete query for every record found. While this is very useful if you want to
delete a subset of records in the table, it is very ineffecient when you simply want to delete all records.</p>

<p>If that is the goal, just use:</p>
<pre>
<var>$b </var><kbd>= new </kbd><var>Book</var><kbd>();
</kbd><var>$b</var><kbd>-&gt;</kbd><var><u>truncate</u></var><kbd>();</kbd>
</pre>

<p>This will delete all records in the <kbd>books</kbd> table, reset all in-table-foreign-keys in related tables linking to the <kbd>books</kbd> table, and delete all records in relationship tables referenced by the <var>Book</var> model.</p>

<p class="note">Note that if your relationship tables are used in multiple many-to-many relations, you should not use this method, as it will also delete all relationships between the other models using the same relationship table!</p>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagespath: ''
		});

	});
//-->
</script>
</body>
</html>
